# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=tidy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.8.0
pkgrel=1
pkgdesc="A tool to tidy down your HTML code to a clean style (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.html-tidy.org/"
license=(custom)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-libxslt"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-ninja")
conflicts=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
provides=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
replaces=("${MINGW_PACKAGE_PREFIX}-tidyhtml")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/htacg/tidy-html5/archive/$pkgver.tar.gz")
sha256sums=('59c86d5b2e452f63c5cdb29c866a12a4c55b1741d7025cf2f3ce0cde99b0660e')

prepare() {
  cd "${srcdir}/tidy-html5-${pkgver}"
}

build() {
  [[ -d "$srcdir}/build-${CARCH}" ]] && rm -rf "$srcdir/build-${CARCH}"
  mkdir -p "$srcdir/build-${CARCH}" && cd "$srcdir/build-${CARCH}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
    -G'Ninja' \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_{SHARED,STATIC}_LIBS=ON \
    -DBUILD_TAB2SPACE=ON \
    ../tidy-html5-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/cmake.exe --install ./

# Compatibility symlinks until everything is ported
  ln -s tidybuffio.h "$pkgdir"/${MINGW_PREFIX}/include/buffio.h
  ln -s tidyplatform.h "$pkgdir"/${MINGW_PREFIX}/include/platform.h

  install -Dm755 tab2space.exe $pkgdir/${MINGW_PREFIX}/bin
  install -Dm644 "$srcdir"/${_realname}-html5-$pkgver/README/LICENSE.md \
    "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
